package top.lishuoboy.knife4j.swagger3_0_0.config;

import cn.hutool.core.collection.ListUtil;
import com.github.xiaoymin.knife4j.spring.extension.ApiAuthorExtension;
import com.github.xiaoymin.knife4j.spring.extension.ApiOrderExtension;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import top.lishuoboy.dependency.base.json.HuJsonUtil;

/**
 * @author lishuoboy
 * @date 2022/4/14 0:45
 */
@Slf4j
@Configuration
public class MySwaggerConfig {

    @Bean
    public Docket docket() {
        ApiInfo apiInfo = ApiInfo.DEFAULT;
        log.warn("默认 apiInfo=={}", HuJsonUtil.toJsonPrettyStr(apiInfo));
        /**
         * 默认值
         * apiInfo=={
         *   "version" : "1.0",
         *   "title" : "Api Documentation",
         *   "description" : "Api Documentation",
         *   "termsOfServiceUrl" : "urn:tos",
         *   "license" : "Apache 2.0",
         *   "licenseUrl" : "http://www.apache.org/licenses/LICENSE-2.0",
         *   "contact" : {
         *     "name" : "",
         *     "url" : "",
         *     "email" : ""
         *   },
         *   "vendorExtensions" : [ ]
         * }
         */
        apiInfo = new ApiInfoBuilder()
            .version("1.0")
            .title("标题Api Documentation")
            .description("描述Api Documentation")
            .termsOfServiceUrl("服务条款网址urn:tos")
            .license("许可Apache 2.0")
            .licenseUrl("许可证网址http://www.apache.org/licenses/LICENSE-2.0")
            .contact(new Contact("小石头", "https://lishuoboy.blog.csdn.net/", "lishuoboy@qq.com"))
            .extensions(ListUtil.list(true, new ApiAuthorExtension("ApiAuthorExtension李石头"), new ApiOrderExtension(1)))
            .build();


        Docket docket = new Docket(DocumentationType.OAS_30);
        log.warn("默认  docket=={}", HuJsonUtil.toJsonPrettyStr(docket));
        /**
         * 默认值
         * docket==
         * {
         *   "documentationType" : {
         *     "name" : "openApi",
         *     "version" : "3.0",
         *     "mediaType" : {
         *       "type" : "application",
         *       "subtype" : "json",
         *       "parameters" : { },
         *       "qualityValue" : 1.0,
         *       "charset" : null,
         *       "wildcardType" : false,
         *       "wildcardSubtype" : false,
         *       "concrete" : true,
         *       "subtypeSuffix" : null
         *     }
         *   },
         *   "groupName" : "default",
         *   "enabled" : true
         * }
         */
        docket = docket
            .apiInfo(apiInfo)
            .groupName("全部")
            // 是否启用Swagger 默认 true
//            .enable(true)
            // 设置哪些接口暴露给Swagger展示
            .select()
            // 扫描所有有注解的api，用这种方式更灵活
            .apis(RequestHandlerSelectors.any())
            // 扫描指定包
//            .apis(RequestHandlerSelectors.basePackage("top.lishuoboy.swagger"))
            // 扫描指定的类注解
//            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            // 扫描指定的方法注解
//            .apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))
            // 扫描所有路径
//            .paths(PathSelectors.any())
            // 没研究
//            .paths(PathSelectors.ant("*"))
            // 扫描正则
//            .paths(PathSelectors.regex(""))

            .build()
        ;

        return docket;
    }


    /**
     * 分组研究
     */
    @Bean
    public Docket docketUser() {
        return new Docket(DocumentationType.OAS_30)
            .groupName("用户组")
            .select()
            .apis(RequestHandlerSelectors.basePackage("top.lishuoboy.knife4j.swagger3_0_0.controller"))
            .build();
    }

    /**
     * 用户管理接口(接口设计demo)
     * 因为需要统一为一个json报文，必须带报文头且记录日志。所以不用restful。
     * 统一用请求body，所以不用get，统一用post。
     */
    @Bean
    public Docket docketUserApi() {
        ApiInfo apiInfo = new ApiInfoBuilder()
            .version("1.0")
            .title("**系统接口设计文档——用户管理接口")
            .description("**系统接口设计文档——用户管理接口")
            .termsOfServiceUrl("ip:port/doc.html")
            .contact(new Contact("小石头", "https://lishuoboy.blog.csdn.net/", "lishuoboy@qq.com"))
            .build();
        return new Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo)
            .groupName("demo")
            .select()
            .apis(RequestHandlerSelectors.basePackage("top.lishuoboy.knife4j.swagger3_0_0.demo.controller"))
            .build();
    }
}